package com.demo.blog;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

public class ArticleController extends Controller{

	
	public void queryBQ() {
		String sql = " select groupName from content where groupName is not null  GROUP BY groupName ";
		set("data", Db.find(sql));
		renderJson();
	}
	
	/**
	 *   文章保存
	 */
	public void save() {
		 String taile = getPara("taile");
		 String wzlName = getPara("wzlName");
		 String write = getPara("write");
		 String city = getPara("city");
		 if(city == null) {
			 city = "";
		 }
		 String sql = " INSERT INTO content (title,txt,level,groupName,addTime) VALUES ('"+wzlName+"', '"+taile+"', "+write+", '"+city+"', SYSDATE()) ";
		 int row = Db.update(sql);
		set("state", row);
		renderJson();
	}
	
	
	/**
	 * 	查询列表
	 */
	public void queryTable() {
		 Integer page = getInt("page");
		 Integer limit = getInt("limit");
		 String title = get("title");
		 Integer city = getInt("city");

		String sql = "select * from content ";
		String whereSql =" WHERE 1=1 ";
		if(title != null) {
			whereSql +=" and title like '%"+title+"%'";
		}
		if(city != null && city != -1) {
			whereSql += " and level = '"+city+"' ";
		}
		String count = "select count(1) from content";
		count += whereSql;// 很绕
		sql += whereSql;
		sql += " LIMIT ?,?";
		set("data", Db.find(sql ,(page-1)*10, limit));
		set("code", 0);
		set("count", Db.query(count));
		renderJson();
	}
	
	/**
	 *   删除单个用户
	 */
	public void delectRowTable() {
		String lis = get("lis");
		String sql = "DELETE FROM content WHERE id in ("+lis+")  ";
		Db.update(sql);
		set("success", true);
		renderJson();
	}
	
	
}
